package com.fp.m7.client.view;

import java.util.Date;

import com.fp.m7.client.MyMessages;
import com.fp.m7.client.presenter.RegisterPresenter;
import com.fp.m7.client.widget.InfoPopup;
import com.fp.m7.shared.ValidationConstants;
import com.fp.m7.shared.ValidationError;
import com.fp.m7.shared.ValidationResult;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.logical.shared.AttachEvent;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;

/**
 * Registration form.
 * 
 * @author 7
 */
public class RegisterView extends Composite implements RegisterPresenter.Display {

    private static MyMessages msg = GWT.create(MyMessages.class);

    private TextBox tbUsername;
    private TextBox tbNickname;

    private PasswordTextBox tbPassword;
    private PasswordTextBox tbPasswordRepeated;

    private Image imgCaptcha;
    private TextBox tbCaptcha;
    private String url;

    private Button btnRegister;

    public RegisterView() {
        AbsolutePanel absolutePanel = new AbsolutePanel();
        absolutePanel.setSize("553px", "294px");
        absolutePanel.setStyleName("fillBothDirections");

        Label lblUsername = new Label(msg.username());
        absolutePanel.add(lblUsername, 10, 10);
        lblUsername.setSize("120px", "24px");

        tbUsername = new TextBox();
        absolutePanel.add(tbUsername, 136, 10);
        tbUsername.setSize("192px", "16px");

        Label lblNickname = new Label(msg.nickname());
        absolutePanel.add(lblNickname, 10, 48);
        lblNickname.setSize("120px", "24px");

        tbNickname = new TextBox();
        absolutePanel.add(tbNickname, 136, 44);
        tbNickname.setSize("192px", "16px");

        Label lblPassword = new Label(msg.password());
        absolutePanel.add(lblPassword, 10, 106);
        lblPassword.setSize("120px", "24px");

        tbPassword = new PasswordTextBox();
        absolutePanel.add(tbPassword, 136, 106);
        tbPassword.setSize("192px", "16px");

        Label lblRepeatPassword = new Label(msg.repeatPassword());
        absolutePanel.add(lblRepeatPassword, 10, 140);
        lblRepeatPassword.setSize("120px", "24px");

        tbPasswordRepeated = new PasswordTextBox();
        absolutePanel.add(tbPasswordRepeated, 136, 140);
        tbPasswordRepeated.setSize("192px", "16px");

        btnRegister = new Button(msg.register());
        absolutePanel.add(btnRegister, 136, 260);
        btnRegister.setSize("200px", "24px");

        imgCaptcha = new Image();
        absolutePanel.add(imgCaptcha, 136, 174);
        imgCaptcha.setSize("200px", "50px");
        imgCaptcha.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent paramClickEvent) {
                refreshCaptcha();
            }
        });

        Label lblCaptcha = new Label(msg.captcha());
        absolutePanel.add(lblCaptcha, 10, 174);
        lblCaptcha.setSize("120px", "24px");

        tbCaptcha = new TextBox();
        absolutePanel.add(tbCaptcha, 136, 230);
        tbCaptcha.setSize("192px", "16px");

        Label lblEnterCaptcha = new Label(msg.enterCaptcha());
        absolutePanel.add(lblEnterCaptcha, 10, 230);
        lblEnterCaptcha.setSize("120px", "24px");

        Label lblUsernameHint = new Label(msg.usernameHint());
        absolutePanel.add(lblUsernameHint, 342, 10);
        lblUsernameHint.setStyleName("registrationHint");
        lblUsernameHint.setSize("200px", "24px");

        Label lblNicknameHint = new Label(msg.nicknameHint());
        absolutePanel.add(lblNicknameHint, 342, 44);
        lblNicknameHint.setStyleName("registrationHint");
        lblNicknameHint.setSize("200px", "24px");

        Label lblCaptchaHint = new Label(msg.captchaHint());
        absolutePanel.add(lblCaptchaHint, 342, 174);
        lblCaptchaHint.setStyleName("registrationHint");
        lblCaptchaHint.setSize("200px", "24px");

        initWidget(absolutePanel);

        addAttachHandler(new AttachEvent.Handler() {
            @Override
            public void onAttachOrDetach(AttachEvent evt) {
                if (evt.isAttached()) {
                    refreshCaptcha();
                }
            }
        });
    }

    private void refreshCaptcha() {
        imgCaptcha.setUrl(url + "?timestamp=" + new Date().getTime());
    }

    @Override
    public void setCaptchaUrl(String url) {
        this.url = url;
        imgCaptcha.setUrl(url);
    }

    @Override
    public HasText getUsername() {
        return tbUsername;
    }

    @Override
    public HasText getPassword() {
        return tbPassword;
    }

    @Override
    public HasText getPasswordRepeated() {
        return tbPasswordRepeated;
    }

    @Override
    public HasText getNickname() {
        return tbNickname;
    }

    @Override
    public HasClickHandlers getSubmitButton() {
        return btnRegister;
    }

    @Override
    public HasText getCaptchaText() {
        return tbCaptcha;
    }

    @Override
    public void clearPasswords() {
        tbPassword.setText("");
        tbPasswordRepeated.setText("");
    }

    @Override
    public void clear() {
        clearPasswords();
        tbUsername.setText("");
        tbNickname.setText("");
    }

    @Override
    public void showErrors(ValidationResult result) {
        for (ValidationError error : result) {
            if (ValidationConstants.Fields.USERNAME.equals(error.getFieldName())) {
                if (ValidationConstants.Messages.NOT_AVAILABLE.equals(error.getErrorMessage())) {
                    InfoPopup.showPopup(tbUsername, msg.usernameNotAvailable());
                } else if (ValidationConstants.Messages.REGEX_FAIL.equals(error.getErrorMessage())) {
                    InfoPopup.showPopup(tbUsername, msg.usernameRegexFail());
                }
            } else if (ValidationConstants.Fields.NICKNAME.equals(error.getFieldName())) {
                if (ValidationConstants.Messages.NOT_AVAILABLE.equals(error.getErrorMessage())) {
                    InfoPopup.showPopup(tbUsername, msg.nicknameNotAvailable());
                } else if (ValidationConstants.Messages.REGEX_FAIL.equals(error.getErrorMessage())) {
                    InfoPopup.showPopup(tbUsername, msg.nicknameRegexFail());
                }
            } else if (ValidationConstants.Fields.NEW_PASSWORD.equals(error.getFieldName())) {
                InfoPopup.showPopup(tbPassword, msg.newPasswordRegexFail());
            } else if (ValidationConstants.Fields.NEW_PASSWORD_REPEATED.equals(error.getFieldName())) {
                InfoPopup.showPopup(tbPasswordRepeated, msg.passwordsDoNotMatch());
            } else if (ValidationConstants.Fields.CAPTCHA.equals(error.getFieldName())) {
                InfoPopup.showPopup(tbCaptcha, msg.captchaIncorrect());
            }
        }
    }
}
